package gui.android;

import database.dbCharacterImpl;
import perv_comp.abbs.cok.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import animal.Adler;
import animal.Elefant;

public class MyCharacter extends Activity implements OnClickListener{

	private Button createButton;
	private RadioButton choiceAdler;
	private RadioButton choiceElefant;
	SQLiteDatabase mydb;
	private  String DBNAME = "PERSONS.db";   
	private  String TABLE = "MY_TABLE"; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
		setContentView(R.layout.chracter);
		choiceAdler = (RadioButton) findViewById(R.id.choiceAdler);
		choiceElefant = (RadioButton) findViewById(R.id.choiceElefant);
		createButton = (Button)findViewById(R.id.create);
		createButton.setOnClickListener(this);
		fillChoices();
	}

	private void fillChoices() {
		Elefant ele1 = new Elefant();
		Adler adl1 = new Adler();
		choiceAdler.setText(Html.fromHtml(adl1.getAnimalStatsHtmlString()));
		choiceElefant.setText(Html.fromHtml(ele1.getAnimalStatsHtmlString()));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.charakter, menu);
		return true;
	}

	@Override
	public void onClick(View component) {
		if (choiceElefant.isChecked()) {
			writeChoiceToDB(1);
		}
		if (choiceAdler.isChecked()) {
			writeChoiceToDB(2);
		}
		switch (component.getId()) {	
		case R.id.create:
			mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
			Cursor c = mydb.rawQuery("SELECT * FROM " + TABLE, null); 
			c.moveToFirst();
			int choice = c.getInt(c.getColumnIndex("CHOICE"));
			int userid = c.getInt(c.getColumnIndex("ID"));
			Log.e("DB Choice", Integer.toString(choice));
			Log.e("DB ID", Integer.toString(userid));
			c.close();
			mydb.close();
			dbCharacterImpl dbC = new dbCharacterImpl();
			dbC.setCharActive(choice, userid, 1);
			Intent intent = new Intent(this, Opponent_List.class);
			startActivity(intent);
			break;
		}
	}

	private void writeChoiceToDB(int choice) {
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		mydb.execSQL("UPDATE " + TABLE + " SET CHOICE="+choice);
		mydb.close();
	}
}
